package com.jinsi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinsi.pojo.ScStudentDefer;
import com.jinsi.service.impl.StuDeferServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StuDeferController {
    @Autowired
    private StuDeferServiceImpl server;

    @ResponseBody
    @RequestMapping("/addDefer")
    public Map<String,String> addDefer(ScStudentDefer defer){
        Map<String, String> map =  new HashMap<>();
        try {
            server.addDefer(defer);
            map.put("state","200");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state","400");
        }
        return map;
    }


    @ResponseBody
    @RequestMapping("/getDefers")
    public Map<String,Object> getDefers(Integer page,Integer limit){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);
        List<ScStudentDefer> list = server.getDefers();
        PageInfo<ScStudentDefer> pageInfo = new PageInfo<>(list);
        map.put("total",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        map.put("state",0);
        map.put("msg","成功");
        return map;
    }


    @ResponseBody
    @RequestMapping("/getDeferByEndTime")
    public Map<String,Object> getDeferByEndTime(Integer page,Integer limit,Integer num){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);
        List<ScStudentDefer> list = server.getDeferByEndTime(num);
        PageInfo<ScStudentDefer> pageInfo = new PageInfo<>(list);
        map.put("total",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        map.put("state",0);
        map.put("msg","成功");
        return map;
    }


    @ResponseBody
    @RequestMapping("/delDefer")
    public Map<String,String> delDefer(Integer id){
        Map<String, String> map = new HashMap<>();
        try {
            server.delDefer(id);
            map.put("state","200");

        } catch (Exception e) {
            e.printStackTrace();
            map.put("state","400");

        }
        return map;
    }


    @ResponseBody
    @RequestMapping("/getDeferByLikeIdAndName")
    public Map<String,Object> getDeferByLikeIdAndName(
            Integer studentId,
            String studentName,
            Integer page,
            Integer limit){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);
        List<ScStudentDefer> list = server.getDeferByLikeIdAndName(studentId,studentName);
        PageInfo<ScStudentDefer> pageInfo = new PageInfo<>(list);
        map.put("total",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        map.put("state",0);
        map.put("msg","成功");
        return map;
    }


    @ResponseBody
    @RequestMapping("/getDeferByTime")
    public Map<String,Object> getDeferByTime(Integer num){
        Map<String, Object> map = new HashMap<>();
        List<ScStudentDefer> list = server.getDeferByEndTime(num);
        if (list.size() > 0){
            map.put("state","200");
        }else {
            map.put("state","400");
        }
        return map;
    }
}
